package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseBindingRecyclerViewAdapter;
import com.cloudcreativity.blyg.databinding.ItemLayoutDateBinding;
import com.cloudcreativity.blyg.databinding.LayoutDatePickerBinding;
import com.cloudcreativity.blyg.entity.Date;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class MyDatePicker extends Dialog {

    public BaseBindingRecyclerViewAdapter<Date, ItemLayoutDateBinding> adapter;
    public ObservableField<String> currentTitle = new ObservableField<>();

    private Calendar current;
    private Calendar today;
    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public MyDatePicker(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(false);
        LayoutDatePickerBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_date_picker,
                null,
                false);
        binding.setUtils(this);
        setContentView(binding.getRoot());
        adapter = new BaseBindingRecyclerViewAdapter<Date, ItemLayoutDateBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_date;
            }

            @Override
            protected void onBindItem(ItemLayoutDateBinding binding, final Date item, int position) {
                binding.setItem(item);
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (onChooseListener != null) {
                            onChooseListener.onChoose(item.getYear(), item.getMonth(), item.getDay());
                            dismiss();
                        }
                    }
                });
            }
        };
        binding.rcvDate.setLayoutManager(new GridLayoutManager(context,
                7, LinearLayoutManager.VERTICAL, false));
        today = Calendar.getInstance(Locale.CHINESE);
        current = Calendar.getInstance(Locale.CHINESE);
        currentTitle.set(current.get(Calendar.YEAR) + "年" + (current.get(Calendar.MONTH) + 1) + "月");
        adapter.getItems().addAll(getAllDays(current));
    }

    public void onOk() {

    }

    public void onClose() {
        dismiss();
    }

    private List<Date> getAllDays(Calendar calendar) {
        int temp = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int i = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.set(Calendar.DAY_OF_MONTH, temp);

        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);


        List<Date> days = new ArrayList<>();
        for (int day = firstDay; day <= lastDay; day++) {
            if (year == today.get(Calendar.YEAR) &&
                    month == today.get(Calendar.MONTH) &&
                    day == today.get(Calendar.DAY_OF_MONTH)) {
                days.add(new Date(year, month, day, false, true));
            } else {
                days.add(new Date(year, month, day, false, false));
            }

        }
        for (int start = 1; start < i; start++) {
            days.add(0, null);
        }
        return days;
    }

    public void onNext() {
        current.add(Calendar.MONTH, 1);
        adapter.getItems().clear();
        adapter.getItems().addAll(getAllDays(current));
        currentTitle.set(current.get(Calendar.YEAR) + "年" + (current.get(Calendar.MONTH) + 1) + "月");
    }

    public void onLast() {
        current.add(Calendar.MONTH, -1);
        adapter.getItems().clear();
        adapter.getItems().addAll(getAllDays(current));
        currentTitle.set(current.get(Calendar.YEAR) + "年" + (current.get(Calendar.MONTH) + 1) + "月");
    }

    public interface OnChooseListener {
        void onChoose(int year, int month, int day);
    }
}
